<template>
	<vue-content>
		<vue-header>
			<vue-header-button>
		        <el-button type="primary" @click="create">新增</el-button>
		    </vue-header-button>
		</vue-header>
		<vue-body>
	    	<el-table
	        v-loading.body="loading"
	        :data="tableData"
	        border
	        style="width: 100%">
	        <el-table-column
	          prop="institutionNo"
	          label="机构编号">
	        </el-table-column>
	        <el-table-column
	          prop="name"
            width="150"
            v-if="bannerType == '01'"
	          label="图片名称">
	        </el-table-column>
          <el-table-column
	          prop="productName"
            v-if="bannerType == '00'"
	          label="商品名称">
	        </el-table-column>
          <el-table-column
	          prop="productNo"
            v-if="bannerType == '00'"
	          label="商品编号">
	        </el-table-column>
	        <el-table-column
	          prop="seq"
            width="80"
	          label="排序">
	        </el-table-column>
          <el-table-column
	          prop="url"
            v-if="bannerType == '01'"
	          label="banner跳转链接" width="180">
	        </el-table-column>
	        <el-table-column width="200"
            v-if="bannerType == '01'"
	          label="显示图片">
	          <template scope="scope">
	            <img class="logoImg" :src="imgSrc(scope.row.picUrl)"/>
	          </template>
	        </el-table-column>
          <el-table-column
	          label="广告状态" width="100">
            <template scope="scope">
              {{ scope.row.status | status }}
            </template>
	        </el-table-column>
	        <el-table-column label="操作" width="220">
          <template scope="scope">
            <el-button-group>
            <el-button
              size="small"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="small"
              type="primary"
              @click="handleDetail(scope.$index, scope.row, tableData)">详情</el-button>
            <el-button
              size="small"
              v-if="scope.row.status == '00'"
              @click="abled(scope.row.id)">启用</el-button>
            <el-button
              size="small"
              type="danger"
              v-if="scope.row.status == '01'"
              @click="enabled(scope.row.id)">禁用</el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index, scope.row, tableData)">删除</el-button>
              </el-button-group>
          </template>
          </el-table-column>
	       </el-table>
	        <div class="back">
            <el-button type="primary" class="insProbtn" @click="back()">后退</el-button>
          </div>
	    </vue-body>
	    <edit-advert
	      ref="edit-advert"
	      @close="editFinish"
	      :view="view"
	      v-if="view ==='create' || view ==='edit'"/>
      <edit-detail
        ref="edit-detail"
        @close="editFinish"
        :view="view"
        v-if="view ==='detail'"/>
   </vue-content>
</template>

<script>
import { queryAdvert,delAdvert,advertDetail,openAdvert,closeAdvert} from "@/services/module";
import {RequestError} from '@/services/errors'
import {deepClone} from '@/utilities/object'
import EditAdvert from './edit'
import EditDetail from './Detail'
export default {
  name: "edit-role",
  components: {EditAdvert,EditDetail},
  data: () => ({
      tableData: [],
      baseData:[],
      loading: false,
      institutionNo:"",
      view: '',
      moduleNo:'',
      bannerType:''
  }),
  props: {},
  created(){
    this.getRouterParams();
  },
  mounted() {
  },
  methods: {
    getRouterParams(){
      // 取到路由带过来的参数 
      let routerParams = this.$route.query;
      // 将数据放在当前组件的数据内
      this.institutionNo = routerParams.institutionNo;
      this.moduleNo = routerParams.moduleNo;
      this.bannerType = routerParams.bannerType;
      this.loadData();
    },
    async loadData () {
        this.loading = true
        try {
          let data = await queryAdvert(Object.assign({currentPage: this.currentPage},{perPage:10},{moduleNo:this.moduleNo,institutionNo:this.institutionNo}))
//        console.log(data)
          this.tableData = data.moduleBannerList
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
    },
    async create () {
        this.view = 'create'
        await this.$nextTick()
        this.$refs['edit-advert'].institutionNo = this.institutionNo;
        this.$refs['edit-advert'].moduleNo = this.moduleNo;
        this.$refs['edit-advert'].bannerType = this.bannerType;
    },
    async handleEdit (index, data) {
        this.view = 'edit'
        await this.$nextTick()
        this.$refs['edit-advert'].dialogFormVisible = true;
        this.$refs['edit-advert'].moduleNo = this.moduleNo;
        this.$refs['edit-advert'].bannerType = this.bannerType;
        this.$refs['edit-advert'].formData = deepClone(data)
        this.$refs['edit-advert'].imgShow = 'edit'
    },
    async handleDetail (index, data) {
      this.view = 'detail'
      await this.$nextTick()
      this.$refs['edit-detail'].bannerType = this.bannerType;
      this.$refs['edit-detail'].id = data.id
      this.$refs['edit-detail'].loadData()
    },
    async enabled (id) {
      try {
        // let insId = id.toString()
        await closeAdvert(id)
        this.$message({
          type: 'success',
          message: '成功'
        })
        this.loadData()
      } catch (e) {
        if (e instanceof RequestError) {
          this.$alert(e.message, '提示', {
            type: 'error',
            confirmButtonText: '确定'
          })
        } else {
          throw e
        }
      }
    },
    async abled (id) {
      try {
        // let insId = id.toString()
        await openAdvert(id)
        this.$message({
          type: 'success',
          message: '成功'
        })
        this.loadData()
      } catch (e) {
        if (e instanceof RequestError) {
          this.$alert(e.message, '提示', {
            type: 'error',
            confirmButtonText: '确定'
          })
        } else {
          throw e
        }
      }
    },
    handleDelete (index, data, rows) {
        this.$confirm(`此操作将删除${data.name}, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          try {
            await delAdvert(data.id)
            this.$message({
              type: 'success',
              message: '已删除'
            })
            rows.splice(index, 1)
          } catch (e) {
            if (e instanceof RequestError) {
              this.$alert(e.message, '提示', {
                type: 'error',
                confirmButtonText: '确定'
              })
            } else {
              throw e
            }
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
    imgSrc(data){
      	return this.$store.state.imgUrl + data;
    },
    editFinish (needRefresh) {
        this.view = ''
        if(needRefresh){
          this.$nextTick(() => {
            // dom元素更新后执行， 因此此处能正确打印出更改之后的值；
            this.loadData()
         	})
        }
    },
    back(){
    	this.$router.go(-1)
    }
  },
  filters: {
      status (value) {
        return {'00': '禁用', '01': '启用', '02': '删除'}[value]
      },
      productType (value) {
        return {1: '实物', 2: '彩票',3:'券/虚拟'}[value]
      }
  }
};
</script>

<style>
	.el-table th>.cell{
		text-align: center;
	}
	.logoImg{
		height: 80px;
	}
	.insProbtn{
		float: right;
		margin-left: 30px;
  }
  .back{
    padding-top: 10px;
  }
</style>